<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工厂" prop="factoryId">
        <el-input v-model="queryParams.factoryId" placeholder="请输入工厂" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="生产工单" prop="moId">
        <el-input v-model="queryParams.moId" placeholder="请输入生产工单" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品" prop="itemWarehouseId">
        <el-input v-model="queryParams.itemWarehouseId" placeholder="请输入产品" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="入库单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入入库单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_APPROVE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="同步ERP" prop="toErp">
        <el-select v-model="queryParams.toErp" placeholder="请选择同步ERP" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_INTEGER_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['wms:produce-in:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['wms:produce-in:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" fixed />
      <el-table-column label="工厂" align="center" prop="factoryName" fixed />
      <el-table-column label="入库单号" align="center" prop="code" fixed width="180px" />
      <el-table-column label="生产工单" align="center" prop="moCode" width="150px" />
      <el-table-column label="产品名称" align="center" prop="itemName" width="100px" />
      <el-table-column label="产品代码" align="center" prop="itemCode" width="100px" />
      <el-table-column label="产品规格" align="center" prop="itemSpec" width="100px" show-overflow-tooltip />
      <el-table-column label="产品图号" align="center" prop="drawCode" width="100px" />
      <el-table-column label="批次号" align="center" prop="batchNo" width="100px" show-overflow-tooltip />
      <el-table-column label="入库数量" align="center" prop="inAmount" />
      <el-table-column label="仓库" align="center" prop="warehouseName" width="100px" />
      <el-table-column label="库区" align="center" prop="areaName" width="100px"  />
      <el-table-column label="库位" align="center" prop="locationName" width="100px"  />
      <el-table-column label="入库日期" align="center" prop="inTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.inTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="制单人" align="center" prop="userName" width="100px"  />
      <el-table-column label="审核人" align="center" prop="approveUserId" width="100px" />
      <el-table-column label="审核时间" align="center" prop="approveTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.approveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_APPROVE" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="同步ERP" align="center" prop="toErp">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_INTEGER_STATUS" :value="scope.row.toErp" />
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="description"  width="150px" show-overflow-tooltip/>
      <el-table-column label="备注" align="center" prop="remark"  width="150px" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150px">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.status == 0" @click="handleUpdate(scope.row)"
                     v-hasPermi="['wms:produce-in:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" v-if="scope.row.status == 0" @click="handleDelete(scope.row)"
                     v-hasPermi="['wms:produce-in:delete']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" v-if="scope.row.status == 0" @click="handleApprove(scope.row)"
                     v-hasPermi="['wms:produce-in:approve']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-close" v-if="scope.row.status == 1" @click="handleCancel(scope.row)"
                     v-hasPermi="['wms:produce-in:approve']">撤审</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库单号" prop="code">
              <genCode v-model="form.code" type="produce-in" :key="refresh"></genCode>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工厂" prop="factoryName">
              <el-input v-model="form.factoryName" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产工单" prop="moId">
              <el-input v-model="form.moCode" placeholder="请选择生产工单" disabled >
                <el-button slot="append" icon="el-icon-search" @click="openSelectSingleMo = true"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产计划" prop="planCode">
              <el-input v-model="form.planCode" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品代码" prop="itemCode">
              <el-input v-model="form.itemCode" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品规格" prop="itemSpec">
              <el-input v-model="form.itemSpec" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品图号" prop="drawCode">
              <el-input v-model="form.drawCode" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="批次号" prop="batchNo">
              <el-input v-model="form.batchNo" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="默认仓库" prop="warehouseArr">
              <selectWarehouse v-model="form.warehouseArr" v-if="editCascaderVisible"></selectWarehouse>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库数量" prop="inAmount">
              <el-input type="number" v-model="form.inAmount" placeholder="请输入入库数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库日期" prop="inTime">
              <el-date-picker clearable v-model="form.inTime" type="date" value-format="timestamp" placeholder="选择入库日期" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :autosize="{minRows: 2, maxRows: 6}" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="说明" prop="description">
              <el-input v-model="form.description" type="textarea" :autosize="{minRows: 2, maxRows: 6}" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查询   -->
    <SelectSingleMo :openSelectSingleMo.sync="openSelectSingleMoQuery" @selectSingleMo = "selectSingleMoQuery"></SelectSingleMo>
    <!-- 新增   -->
    <SelectSingleMo :openSelectSingleMo.sync="openSelectSingleMo" @selectSingleMo = "selectSingleMo"></SelectSingleMo>

  </div>
</template>

<script>
import { createProduceIn, updateProduceIn, deleteProduceIn, getProduceIn, getProduceInPage, exportProduceInExcel, approveProduceIn, cancelProduceIn } from "@/api/wms/produceIn";
import { getItemWarehouseByFactoryAndItem } from '@/api/wms/itemWarehouse'
import SelectFactory from '@/components/SelectFactory/index.vue'
import SelectSingleMo from '@/components/SelectSingleMo/index.vue'
import GenCode from '@/components/GenCode/index.vue'
import SelectWarehouse from '@/components/SelectWarehouse/index.vue'

export default {
  name: "ProduceIn",
  components: {
    SelectWarehouse,
    SelectFactory,
    SelectSingleMo,
    GenCode
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产入库单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSelectSingleMoQuery: false,
      openSelectSingleMo: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        factoryId: null,
        moId: null,
        itemWarehouseId: null,
        code: null,
        batchNo: null,
        status: null,
        toErp: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 单号刷新
      refresh: undefined,
      // 强制刷新仓库选择组件
      editCascaderVisible: true,
      // 表单校验
      rules: {
        factoryId: [{ required: true, message: "工厂不能为空", trigger: "blur" }],
        planId: [{ required: true, message: "生产计划不能为空", trigger: "blur" }],
        moId: [{ required: true, message: "生产工单不能为空", trigger: "blur" }],
        itemId: [{ required: true, message: "产品不能为空", trigger: "blur" }],
        itemWarehouseId: [{ required: true, message: "产品不能为空", trigger: "blur" }],
        code: [{ required: true, message: "入库单号不能为空", trigger: "blur" }],
        inAmount: [{ required: true, message: "入库数量不能为空", trigger: "blur" }],
        warehouseId: [{ required: true, message: "仓库不能为空", trigger: "blur" }],
        inTime: [{ required: true, message: "入库日期不能为空", trigger: "blur" }],
        userId: [{ required: true, message: "制单人不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "change" }],
        toErp: [{ required: true, message: "同步ERP不能为空", trigger: "change" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getProduceInPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    //查询工单
    selectSingleMoQuery(data){
      this.queryParams.moId = data.id;
      this.queryParams.moCode = data.code;
    },
    selectSingleMo(data){
      this.form.moId = data.id;
      this.form.moCode = data.code;
      this.form.planId = data.planId;
      this.form.planCode = data.planCode;
      this.form.itemName = data.itemName;
      this.form.itemId = data.itemId;
      this.form.itemCode = data.itemCode;
      this.form.itemSpec = data.spec;
      this.form.drawCode = data.drawCode;
      this.form.batchNo = data.batchNo;
      this.form.inAmount = data.reportAmount;
      this.form.factoryId = data.factoryId;
      this.form.factoryName = data.factoryName;
      this.form.remark = data.remark;
      this.activeMo = data;
      this.getItemWarehouseByFactoryAndItemFun(data.factoryId, data.itemId);
    },
    getItemWarehouseByFactoryAndItemFun(factoryId, itemId) {
      const that = this
      getItemWarehouseByFactoryAndItem(factoryId, itemId).then(response => {
        const data = response.data;
        if (data == undefined || data == null) {
          this.$modal.confirm('未查询到相关的产品仓库信息，是否前往维护？').then(function() {
            that.cancel();
            that.$router.push("/wms/item-warehouse");
          }).catch((e) => {
          });
        }else {
          this.form.itemWarehouseId = data.id;
          this.form.warehouseId = data.warehouseId;
          this.form.areaId = data.areaId;
          this.form.locationId = data.locationId;
          //处理仓库数据
          this.form.warehouseArr = [];
          if (this.form.warehouseId != null && this.form.warehouseId != undefined && this.form.warehouseId != 0) {
            this.form.warehouseArr.push("0-" + this.form.warehouseId);
          }
          if (this.form.areaId != null && this.form.areaId != undefined&& this.form.areaId != 0) {
            this.form.warehouseArr.push("1-" + this.form.areaId);
          }
          if (this.form.locationId != null && this.form.locationId != undefined&& this.form.locationId != 0) {
            this.form.warehouseArr.push("2-" + this.form.locationId);
          }
          this.editCascaderVisible = false;
          this.$nextTick(() => {
            this.editCascaderVisible = true;
          })
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        factoryId: undefined,
        planId: undefined,
        moId: undefined,
        itemId: undefined,
        itemWarehouseId: undefined,
        code: undefined,
        batchNo: undefined,
        inAmount: undefined,
        warehouseArr: [],
        warehouseId: undefined,
        areaId: undefined,
        locationId: undefined,
        inTime: new Date().getTime(),
        userId: undefined,
        deptId: undefined,
        approveUserId: undefined,
        approveTime: undefined,
        status: 0,
        toErp: 1,
        description: undefined,
        remark: undefined,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产入库单";
      this.refresh = new Date().getTime();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getProduceIn(id).then(response => {
        this.form = response.data;
        this.getItemWarehouseByFactoryAndItemFun(this.form.factoryId, this.form.itemId)
        this.open = true;
        this.title = "修改生产入库单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 处理仓库
        this.form.warehouseId = 0;
        this.form.areaId = 0;
        this.form.locationId = 0;
        if (this.form.warehouseArr[0] != null && this.form.warehouseArr[0] != undefined) {
          this.form.warehouseId =  this.form.warehouseArr[0].split("-")[1];
        }
        if (this.form.warehouseArr[1]!= null && this.form.warehouseArr[1] != undefined) {
          this.form.areaId =  this.form.warehouseArr[1].split("-")[1];
        }
        if (this.form.warehouseArr[2] != null && this.form.warehouseArr[2] != undefined) {
          this.form.locationId =  this.form.warehouseArr[2].split("-")[1];
        }
        // 修改的提交
        if (this.form.id != null) {
          updateProduceIn(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createProduceIn(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除生产入库单编号为"' + id + '"的数据项?').then(function() {
          return deleteProduceIn(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 审核操作 */
    handleApprove(row) {
      const id = row.id;
      this.$modal.confirm('是否确认审核入库单编号为"' + id + '"的数据项?').then(function() {
        return approveProduceIn(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {});
    },
    /*  撤销审核 */
    handleCancel(row) {
      const id = row.id;
      this.$modal.confirm('是否确认撤销入库单编号为"' + id + '"的数据项?').then(function() {
        return cancelProduceIn(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤审成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有生产入库单数据项?').then(() => {
          this.exportLoading = true;
          return exportProduceInExcel(params);
        }).then(response => {
          this.$download.excel(response, '生产入库单.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
